home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_203 / gurusguide / cia.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  142 lines

  1. /************************************************************************
  2. **********                                                     **********
  3. **********              C I A   I N T E R R U P T              **********
  4. **********              -------------------------              **********
  5. **********                                                     **********
  6. **********        Copyright (C) 1988 Sassenrath Research       **********
  7. **********                All Rights Reserved.                 **********
  8. **********                                                     **********
  9. **********    Example from the "Guru's Guide, Meditation #1"   **********
  10. **********                                                     **********
  11. *************************************************************************
  12. **                                                                     **
  13. **                            - NOTICE -                               **
  14. **                                                                     **
  15. **  The "Guru's Guide, Meditation #1" contains detailed information    **
  16. **  about Amiga interrupts as well as a complete discussion of this    **
  17. **  and other examples.  Meditation #1 and all of its examples were    **
  18. **  written by Carl Sassenrath, the architect of Amiga's multitasking  **
  19. **  operating system.  Copies of the "Guru's Guide" may be obtained    **
  20. **  from:                                                              **
  21. **           GURU'S GUIDE, P.O. BOX 1510, UKIAH, CA 95482              **
  22. **                                                                     **
  23. **  Please include a check for $14.95, plus $1.50 shipping ($4.00 if   **
  24. **  outside North America).  CA residents add 6% sales tax.            **
  25. **                                                                     **
  26. **  This example may be used for any purposes, commercial, personal,   **
  27. **  public, and private, so long as ALL of the above text, copyright,  **
  28. **  mailing address, and this notice are retained in their entirety.   **
  29. **                                                                     **
  30. **  THIS EXAMPLE IS PROVIDED WITHOUT WARRANTY OF ANY KIND.             **
  31. **                                                                     **
  32. ************************************************************************/
  33.  
  34. /*
  35. **  COMPILATION NOTE:
  36. **
  37. **  Compiled under MANX AZTEC C 3.6A.  Use the +L compiler option
  38. **  and the "c32" library.  Link with intrsup.o.
  39. */
  40.  
  41.  
  42. #include <exec/exec.h>
  43. #include <hardware/intbits.h>
  44. #include <hardware/cia.h>
  45.  
  46. struct Task *ATask = NULL;
  47. struct Interrupt *Intr = NULL;
  48. APTR Cia = NULL;
  49. long ASignal = -1;
  50.  
  51. long Counter = 10;
  52.  
  53.  
  54. /* Interrupt Processing Code */
  55. VOID IntrProc()
  56. {
  57.     int_start();
  58.  
  59.     /* Clear timer-A interrupt */
  60.     SetICR(Cia, CIAICRF_TA);
  61.  
  62.     if (--Counter <= 0)
  63.     {
  64.         /* Disable timer interrupt */
  65.         AbleICR(Cia, CIAICRF_TA);
  66.  
  67.         Signal(ATask,1 << ASignal);
  68.     }
  69.  
  70.     int_end();
  71. }
  72.  
  73.  
  74. main()
  75. {
  76.     MainInit();
  77.  
  78.     if (AddICRVector(Cia,CIAICRB_TA,Intr) != 0)
  79.     {
  80.         puts("CIA-B Timer-A in use.");
  81.         MainExit(300);
  82.     }
  83.  
  84.     /* At this point, Timer intr is linked  */
  85.     /* and enabled, so it may have happened */
  86.     /* already!  That's why we need this:   */
  87.     ciab.ciacra &= ~CIACRAF_START;    /* stop timer */
  88.     SetICR(Cia,CIAICRF_TA);        /* clear intr */
  89.     SetSignal(0, 1 << ASignal);    /* clear signal */
  90.  
  91.     /* Set timer counter latch */
  92.     ciab.ciatalo = 0;
  93.     ciab.ciatahi = 0xff;
  94.  
  95.     /* Start the timer */
  96.     ciab.ciacra |= CIACRAF_START;
  97.  
  98.     while (Counter > 0)
  99.         printf("%d %2x%02x\n", Counter, ciab.ciatahi, ciab.ciatalo);
  100.  
  101.     Wait(1 << ASignal);    /* Sync-up */
  102.  
  103.     ciab.ciacra &= ~CIACRAF_START;    /* stop timer */
  104.  
  105.     RemICRVector(Cia,CIAICRB_TA,Intr);
  106.  
  107.     MainExit(0);
  108. }
  109.  
  110.  
  111. MainInit()
  112. {
  113.     extern APTR OpenResource();
  114.     extern struct Interrupt *MakeIntr();
  115.     extern struct Task *FindTask();
  116.     extern int Enable_Abort;
  117.  
  118.     Enable_Abort = 0;    /* prevent a CTRL-C */
  119.  
  120.     Cia = OpenResource("ciab.resource", 0);
  121.     if (Cia == NULL) MainExit(201);
  122.  
  123.     Intr = MakeIntr("cia.example",0,&IntrProc,0);
  124.     if (Intr == NULL) MainExit(202);
  125.  
  126.     ASignal = AllocSignal(-1);
  127.     if (ASignal == -1) MainExit(203);
  128.  
  129.     ATask = FindTask(NULL);
  130. }
  131.  
  132.  
  133. MainExit(error)
  134.     int error;
  135. {
  136.     FreeIntr(Intr);
  137.  
  138.     if (ASignal != -1) FreeSignal(ASignal);
  139.  
  140.     exit(error);
  141. }
  142.